//
//  CFontPickerViewController.m
//  FontPicker
//
//  Created by 为为 on 13-6-9.
//  Copyright (c) 2013年 为为. All rights reserved.
//

#import "CFontPickerViewController.h"

@interface CFontPickerViewController ()

@end

@implementation CFontPickerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
		_selectedRow = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	pickerView.dataSource = self;
	pickerView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadDefaulFontArray
{
	NSMutableArray* array = [NSMutableArray array];
	NSArray *familyNames = [UIFont familyNames];
    for( NSString *familyName in familyNames ){
      
        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
        if (!fontNames || fontNames.count==0) {
            continue;
        }
        NSString* shortestFontName = [fontNames objectAtIndex:0];
        for( NSString *fontName in fontNames ){
            if (fontName.length < shortestFontName.length) {
                shortestFontName = fontName;
            }
        }
        [array addObject:shortestFontName];
    }
	[array sortUsingComparator:^(id obj1, id obj2) {
		NSString* str1 = obj1;
		NSString* str2 = obj2;
		return (NSComparisonResult)strcmp([str1 UTF8String], [str2 UTF8String]);
	}];
	self.fontArray = array;
}

- (void)selectRow:(int)index animated:(BOOL)animated
{
	[pickerView selectRow:index inComponent:0 animated:animated];
}

#pragma mark picker view datasource

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return [self.fontArray count];
}

#pragma mark picker view delegate

- (UIView *)pickerView:(UIPickerView *)apickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
	UILabel* label = nil;
	
	if (!view) {
		label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 40)];
		label.backgroundColor = [UIColor clearColor];
		label.textColor = [UIColor blackColor];
		label.textAlignment = NSTextAlignmentCenter;
	} else {
		label = (UILabel*)view;
	}
	
	NSString* fontName = [self.fontArray objectAtIndex:row];
	label.text = fontName;
	label.font = [UIFont fontWithName:fontName size:label.font.pointSize];
	
	return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	_selectedRow = (int)row;
	if (self.rowSelectedBlock) {
		self.rowSelectedBlock(self, (int)row);
	}
}

@end
